'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 开服狂欢
 * @author sundbcn@gmail.com (sun debing)
 */

var dataApi = require('../libs/dataApi');
var _ = require('lodash');
var moment = require('moment');
var open = require('../../config/development/open.json');

/**
 * 获取当前可显示列表
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
// exports.getOpenServerList = function () {
//     var list = [];
//     var star_time = open.open_time;
//     var now_time = moment().unix();
//     var end_time = star_time + 24 * 3600 * 7;
//     var isVail = true;

//     if (now_time >= end_time) {
//         isVail = false;
//     }

//     var carnival_conf = dataApi.open_seven_day.all();
//     if (!carnival_conf) {
//         console.error('carnival_conf type can not found:');
//         return list;
//     }
//     if (!!isVail) {
//         var carnival_list = _.clone(carnival_conf, true);
//         _.each(carnival_list, function (value, key) {
//             _.each(value, function (value1) {
//                 var conf_time = value1.open_time;
//                 value1.state = 0;
//                 var isVail2 = 1;
//                 var isVail1 = now_time - star_time >= value1.open_time;
//                 if (!isVail1) {
//                     isVail2 = 2;
//                 }
//                 if (isVail2 == 1) {
//                     list.push(value1);
//                 }
//             });
//         });
//     }
//     return list;
// };

exports.getOpenServerList = function () {
    var list = [];
    var star_time = open.open_time;
    var now_time = moment().unix();
    var end_time = star_time + 24 * 3600 * 7;
    var isVail = true;

    if (now_time >= end_time) {
        isVail = false;
    }

    var carnival_conf = dataApi.open_seven_day.all();
    if (!carnival_conf) {
        console.error('carnival_conf type can not found:');
        return list;
    }
    if (!!isVail) {
        var carnival_list = _.clone(carnival_conf, true);
        _.each(carnival_list, function (value, key) {
            _.each(value, function (value1) {
                var conf_time = value1.open_time;
                value1.state = 0;
                var isVail_state = now_time - star_time >= value1.open_time;
                if (!!isVail_state) {
                    value1.state = 2;
                }
                list.push(value1);
            });
        });
    }
    return list;
};

/**
 * 获取当前可显示列表
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getOpenServerRewardList = function (carnival_id) {
    var conf_list = dataApi.open_seven_day_reward.findById(carnival_id);
    if (!conf_list) {
        console.error('carnival_id type can not found:' + carnival_id);
        return;
    } else {
        return conf_list;
    }
};

/**
 * 获取value1列表
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getValueList = function (type) {
    var carnival_conf = dataApi.open_seven_day.all();
    var carnival_list = _.clone(carnival_conf, true);
    var list = [];
    _.each(carnival_list, function (value, key) {
        _.each(value, function (value1) {
            if (value1.type == type) {
                list.push(value1.value1);
            }

        });
    });
    return list;
};

// 俩个value特殊处理 士兵星级个数
exports.getValueList1 = function (type) {
    var carnival_conf = dataApi.open_seven_day.all();
    var carnival_list = _.clone(carnival_conf, true);
    var list = [];
    _.each(carnival_list, function (value, key) {
        _.each(value, function (value1) {
            if (value1.type == type) {
                list.push(value1.value2);
            }

        });
    });
    return list;
};

/**
 * 通过类型和value1值获取任务id
 * @param  {[type]} vip [description]
 * @return {[type]}     [description]
 */
exports.getTypeByValue4 = function (type, value4) {
    var carnival_conf = dataApi.open_seven_day.all();
    var carnival_item = _.clone(carnival_conf, true);
    var carnival_id = 0;
    _.each(carnival_item, function (value) {
        _.each(value, function (value3) {
            if (value3.type == type) {
                if (value3.value1 == value4) {
                    carnival_id = value3.id;
                }
            }
        });
    });
    return carnival_id;
};
